package cn.iocoder.yudao.module.cook.controller.admin.tiaoliao;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.cook.controller.admin.tiaoliao.vo.*;
import cn.iocoder.yudao.module.cook.dal.dataobject.tiaoliao.TiaoliaoDO;
import cn.iocoder.yudao.module.cook.service.tiaoliao.TiaoliaoService;

@Tag(name = "管理后台 - 食谱调料")
@RestController
@RequestMapping("/cook/tiaoliao")
@Validated
public class TiaoliaoController {

    @Resource
    private TiaoliaoService tiaoliaoService;

    @PostMapping("/create")
    @Operation(summary = "创建食谱调料")
    @PreAuthorize("@ss.hasPermission('cook:tiaoliao:create')")
    public CommonResult<Long> createTiaoliao(@Valid @RequestBody TiaoliaoSaveReqVO createReqVO) {
        return success(tiaoliaoService.createTiaoliao(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新食谱调料")
    @PreAuthorize("@ss.hasPermission('cook:tiaoliao:update')")
    public CommonResult<Boolean> updateTiaoliao(@Valid @RequestBody TiaoliaoSaveReqVO updateReqVO) {
        tiaoliaoService.updateTiaoliao(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除食谱调料")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('cook:tiaoliao:delete')")
    public CommonResult<Boolean> deleteTiaoliao(@RequestParam("id") Long id) {
        tiaoliaoService.deleteTiaoliao(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得食谱调料")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('cook:tiaoliao:query')")
    public CommonResult<TiaoliaoRespVO> getTiaoliao(@RequestParam("id") Long id) {
        TiaoliaoDO tiaoliao = tiaoliaoService.getTiaoliao(id);
        return success(BeanUtils.toBean(tiaoliao, TiaoliaoRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得食谱调料分页")
    @PreAuthorize("@ss.hasPermission('cook:tiaoliao:query')")
    public CommonResult<PageResult<TiaoliaoRespVO>> getTiaoliaoPage(@Valid TiaoliaoPageReqVO pageReqVO) {
        PageResult<TiaoliaoDO> pageResult = tiaoliaoService.getTiaoliaoPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, TiaoliaoRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出食谱调料 Excel")
    @PreAuthorize("@ss.hasPermission('cook:tiaoliao:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportTiaoliaoExcel(@Valid TiaoliaoPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<TiaoliaoDO> list = tiaoliaoService.getTiaoliaoPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "食谱调料.xls", "数据", TiaoliaoRespVO.class,
                        BeanUtils.toBean(list, TiaoliaoRespVO.class));
    }

}